package com.hongyu.smart.village.ui.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hongyu.smart.village.listener.OnItemClickListener;

import java.util.List;

public abstract class BaseAdapter<T,E extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<E> {
  protected Context mContext;
  protected List<T> mList;
  private OnItemClickListener mOnItemClickListener;

  public BaseAdapter(Context mContext, List<T> mList){
    this.mContext=mContext;
    this.mList=mList;
  }
  @NonNull
  @Override
  public E onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(mContext).inflate(getItemView(),parent,false);
    return getViewHolder(view);
  }

  protected abstract E getViewHolder(View itemView);

  protected abstract int getItemView();

  @Override
  public void onBindViewHolder(@NonNull E holder, @SuppressLint("RecyclerView") int position) {
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (mOnItemClickListener!=null){
          mOnItemClickListener.onItemClick(position);
        }
      }
    });
    onBindHolder(holder,position);

  }

  protected abstract void onBindHolder(E holder, int position);

  @Override
  public int getItemCount() {
    return mList.size();
  }

  public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
    this.mOnItemClickListener = mOnItemClickListener;
  }
}
